package store

import (
	"fmt"
	"runtime"
)

type Log struct {
}

type LogBucket struct {
	Redis
	Level string
}

func (s *LogBucket) SetContent(content string) {
	strEam := make(map[string]string)
	strEam["level"] = s.Level
	strEam["server"] = s.conf.Get().Name
	strEam["content"] = content
	_ = s.Redis.Publish(LogerTopic, strEam)
	return
}

func Eprintln(format string, v ...interface{}) string {
	var buf [2 << 10]byte
	format = format + " stack:%s"
	v = append(v, string(buf[:runtime.Stack(buf[:], true)]))
	return fmt.Sprintf(format, v...)
}

func (s *LogBucket) Debug(format string, v ...interface{}) {
	s.Level = "debug"
	s.SetContent(fmt.Sprintf(format, v...))
}

func (s *LogBucket) Error(format string, v ...interface{}) {
	s.Level = "error"
	s.SetContent(Eprintln(format, v))
}

func (s *LogBucket) Info(format string, v ...interface{}) {
	s.Level = "info"
	s.SetContent(fmt.Sprintf(format, v...))
}

func (s *Log) Get() *LogBucket {
	logOnce.Do(func() {
		//logs.SetLevel(0)
		logClient = &LogBucket{}
		//err := logClient.SetLogger(logs.AdapterFile, fmt.Sprintf(`{"filename":"%s"}`, s.Config.Get().Log.Path))
		//if err != nil {
		//	panic(err)
		//}
		//logClient.EnableFuncCallDepth(true)
	})
	return logClient
}
